package org.songwang.org.service.impl;

import org.songwang.basic.PageList;
import org.songwang.basic.mapper.BaseMapper;
import org.songwang.basic.query.BaseQuery;
import org.songwang.org.domain.Employee;
import org.songwang.org.domain.Shop;
import org.songwang.org.mapper.IEmployeeMapper;
import org.songwang.org.mapper.IShopMapper;
import org.songwang.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl implements IShopService {
    @Autowired
    private IShopMapper shopMapper;
    @Autowired
    private IEmployeeMapper employeeMapper;
    @Override
    public List<Shop> findAll() {
        return shopMapper.findAll();
    }

    @Override
    public Shop findById(Long id) {
        return shopMapper.findById(id);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        shopMapper.delete(id);
    }

    @Override
    @Transactional
    public void add(Shop shop) {
        shopMapper.add(shop);
    }

    @Override
    @Transactional
    public void update(Shop shop) {
        shopMapper.update(shop);
    }

    @Override
    public PageList findQuery(BaseQuery query) {
        Long count = shopMapper.count(query);
        List<Shop> query1 = shopMapper.findQuery(query);

        return new PageList<Shop>(count,query1);
    }

    @Override
    public Long count(BaseQuery query) {
        return shopMapper.count(query);
    }

    @Override
    public void settlement(Shop shop) {
        //获取shop里面的admin对象
        Employee admin = shop.getAdmin();
        //添加admin对象
       employeeMapper.add(admin);
       //获取admin的主键id。设置到shop对象的admin_id的属性上
       shop.setAdmin_id(admin.getId());
       //添加shop对象
       shopMapper.add(shop);
       //获取添加后shop对象的主键id。设置到admin对象的shop_id属性上面去
       admin.setShop_id(shop.getId());
       //修改
       employeeMapper.update(admin);
    }
}
